package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bean.dto.Dto;
import com.bean.pojo.*;
import com.bean.vo.userinfo.ItripAddUserLinkUserVO;
import com.bean.vo.userinfo.ItripModifyUserLinkUserVO;
import com.bean.vo.userinfo.ItripSearchUserLinkUserVO;
import com.service.IItripOrderLinkUserService;
import com.service.IItripTradeEndsService;
import com.service.IItripUserLinkUserService;
import com.service.IItripUserService;
import com.util.DtoUtil;
import com.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@RestController
public class UserInfoController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private IItripUserLinkUserService iItripUserLinkUserService;
    @Autowired
    private IItripUserService iItripUserService;
    @Autowired
    private IItripOrderLinkUserService iItripOrderLinkUserService;
    @Autowired
    private IItripTradeEndsService iItripTradeEndsService;

    @PostMapping("/api/userinfo/adduserlinkuser")
    public @ResponseBody
    Dto itripAddUserLinkUserVO(
            @RequestBody ItripAddUserLinkUserVO itripAddUserLinkUserVO, HttpServletRequest request) {
        System.out.println(">>> 新增常用联系人接口");
        //100000 : token失效，请重登录
        //获取token字符串
        String istoken = request.getHeader("token");
        String string = null;
        try {
            string = redisUtil.getString(istoken);
        } catch (Exception e) {
            return DtoUtil.returnError("100000", "token失效，请重登录", "");
        }
        System.out.println(string);
        if (string == null) {
            return DtoUtil.returnError("100000", "token失效，请重登录", "");
        }

        System.out.println("人:" + itripAddUserLinkUserVO);

        ItripUserLinkUser user = new ItripUserLinkUser();
        user.setLinkUserName(itripAddUserLinkUserVO.getLinkUserName());
        user.setLinkPhone(itripAddUserLinkUserVO.getLinkPhone());
        user.setLinkIdCard(itripAddUserLinkUserVO.getLinkIdCard());
        user.setCreationDate(new Date());
        user.setLinkIdCardType(0);

        //获取token字符串
        StringBuffer token = new StringBuffer();
        token.append(request.getHeader("token"));
        //根据token字符串查询添加用户id
        QueryWrapper<ItripUser> wrapper = new QueryWrapper<>();
        String userCode = token.substring(token.indexOf("-") + 1);
        userCode = userCode.substring(0, userCode.indexOf("-"));

        wrapper.eq("userCode", userCode);
        ItripUser one = iItripUserService.getOne(wrapper);

        System.out.println(one);

        user.setCreatedBy(one.getId());
        user.setUserId(one.getId());
        //非空判断
        if (itripAddUserLinkUserVO.getLinkUserName() == null || itripAddUserLinkUserVO.getLinkPhone() == null
                || itripAddUserLinkUserVO.getLinkIdCard() == null || itripAddUserLinkUserVO.getLinkUserName().equals("")
                || itripAddUserLinkUserVO.getLinkPhone().equals("") || itripAddUserLinkUserVO.getLinkIdCard().equals("")) {
            return DtoUtil.returnError("100412 ", "不能提交空，请填写常用联系人信息", "");
        }

        boolean save = iItripUserLinkUserService.save(user);

        if (save) {
            return DtoUtil.returnSuccess("添加成功");
        } else {
            return DtoUtil.returnError("100000", "token失效，请重登录", "");
        }
    }


    @GetMapping("/api/userinfo/deluserlinkuser")
    public @ResponseBody
    Dto deluserlinkuser(Integer[] ids,HttpServletRequest request) {
        System.out.println(">>删除常用联系人接口");
        //100000 : token失效，请重登录
            //获取token字符串
        //获取token字符串
        String istoken = request.getHeader("token");
        String string = null;
        try {
            string = redisUtil.getString(istoken);
        } catch (Exception e) {
            return DtoUtil.returnError("100000", "token失效，请重登录", "");
        }
        System.out.println(string);
        if (string == null) {
            return DtoUtil.returnError("100000", "token失效，请重登录", "");
        }

        System.out.println("接受到的数据" + Arrays.toString(ids));
        //100433 : 请选择要删除的常用联系人
        if(ids==null){
            return DtoUtil.returnError("100433", "请选择要删除的常用联系人", "");
        }

        //100431 : 所选的常用联系人中有与某条待支付的订单关联的项，无法删除
        for (int i = 0; i < ids.length; i++) {
            //找到单个用户id所有订单对象
            QueryWrapper<ItripOrderLinkUser> wrapper = new QueryWrapper<>();
            wrapper.eq("linkUserId", ids[i]);
            List<ItripOrderLinkUser> list = iItripOrderLinkUserService.list(wrapper);
            //输出单个用户所有订单信息
            for (ItripOrderLinkUser iol:
            list) {
                System.out.println(iol);
            }

            //根据单个用户所有订单信息查询支付信息
            for (ItripOrderLinkUser iol:
            list) {
                System.out.println("查找的支付id"+iol.getOrderId());
                QueryWrapper<ItripTradeEnds> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("id", iol.getOrderId());

                ItripTradeEnds one = iItripTradeEndsService.getOne(wrapper1);
                if(one==null) continue;

                System.out.println("打印单个订单信息"+iol.getLinkUserName()+" "+one.getFlag());
                if(one.getFlag()!=2){
                    return DtoUtil.returnError("100431", "所选的常用联系人中有与某条待支付的订单关联的项，无法删除", "");
                }
            }
        }

//        100432 : 删除常用联系人失败
        for (Integer i:
        ids) {
            boolean b = iItripUserLinkUserService.removeById(i);
            if(!b){
                return DtoUtil.returnError("100432", "删除常用联系人失败", "");
            }
        }

        return DtoUtil.returnSuccess("删除成功!", "");
    }

    @PostMapping("/api/userinfo/queryuserlinkuser")
    public @ResponseBody
    Dto queryuserlinkuser(@RequestBody ItripSearchUserLinkUserVO itripSearchUserLinkUserVO, HttpServletRequest request) {
        //获取token字符串
        String istoken = request.getHeader("token");
        String string = null;
        try {
            string = redisUtil.getString(istoken);
        } catch (Exception e) {
            return DtoUtil.returnError("100000", "token失效，请重登录", "");
        }
        System.out.println(string);
        if (string == null) {
            return DtoUtil.returnError("100000", "token失效，请重登录", "");
        }

        System.out.println(">>查询常用联系人接口");
        System.out.println(itripSearchUserLinkUserVO);

        QueryWrapper<ItripUserLinkUser> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(itripSearchUserLinkUserVO.getLinkUserName()), "linkUserName",
                itripSearchUserLinkUserVO.getLinkUserName());

        List<ItripUserLinkUser> list = null;
        try {
            list = iItripUserLinkUserService.list(wrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnError("100401", "获取常用联系人信息失败", "");
        }

        if (list.size() >= 0) {
            return DtoUtil.returnSuccess("查询成功!", list);
        } else {
            return DtoUtil.returnError("100000", "token失效，请重登录", "");
        }
    }

    @PostMapping("/api/userinfo/modifyuserlinkuser")
    public @ResponseBody
    Dto modifyuserlinkuser(@RequestBody ItripModifyUserLinkUserVO itripModifyUserLinkUserVO, HttpServletRequest request) {
        System.out.println(">>修改联系人");
        //获取token字符串
        String istoken = request.getHeader("token");
        String string = null;
        try {
            string = redisUtil.getString(istoken);
        } catch (Exception e) {
            return DtoUtil.returnError("100000", "token失效，请重登录", "");
        }
        System.out.println(string);
        if (string == null) {
            return DtoUtil.returnError("100000", "token失效，请重登录", "");
        }


        System.out.println(itripModifyUserLinkUserVO);

        if(itripModifyUserLinkUserVO.getLinkUserName()==null||
                itripModifyUserLinkUserVO.getLinkPhone()==null||
                itripModifyUserLinkUserVO.getLinkIdCard()==null){
            return DtoUtil.returnError("100422", "不能提交空，请填写常用联系人信息", "");
        }

        //根据id查单个联系人
        QueryWrapper<ItripUserLinkUser> wrapper = new QueryWrapper<>();
        wrapper.eq("id", itripModifyUserLinkUserVO.getId());
        ItripUserLinkUser one = iItripUserLinkUserService.getOne(wrapper);

        one.setLinkUserName(itripModifyUserLinkUserVO.getLinkUserName());
        one.setLinkPhone(itripModifyUserLinkUserVO.getLinkPhone());
        one.setLinkIdCard(itripModifyUserLinkUserVO.getLinkIdCard());
        one.setModifyDate(new Date());

        boolean b = iItripUserLinkUserService.updateById(one);

        if (b) {
            return DtoUtil.returnSuccess("修改成功!", "");
        } else {
            return DtoUtil.returnError("100421", "修改常用联系人失败", "");
        }
    }


}
